#include <iostream>
#include <stdlib.h>
#include <time.h>
#include "d666.h"


int d666()
    {
        int u , t , h ;	// number of units, tens and hundreds
        int result ;		// final result

        srand(time(NULL)) ;

        u = rand() % 6 + 1;			//  Calculation of the units
        t = (rand() % 6 + 1) * 10;	// Calculation of the 1O's
        h = (rand() % 6 + 1) * 100;	// Calculation of the 100's

        result = u + h + t ;	// Calculation of the final result
        return result ;
    }

int divineTable(int difficulty, int testLevel)
    {
    	int result ;	// The final result under 7 is fail above is ok

    	int d666Result = d666() ;		// Let's roll the dice

    	if (d666Result == 666)
    	{
			result = 0 ;	// Supreme FAIL
    	}
    	else if  (d666Result == 111)
    	{
			result = 15 ;	// You invented it !!!
    	}
    	else
		{

			int upperResult, lowerResult ;	// d666Result will be cut in to parts

			upperResult = d666Result / 10 ;	// tens and hundreds on a parts
			lowerResult = d666Result % 10 ;	// units on another part


				// Divine Tables
			int divineTableSet[4] [18] = {31, 43, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 16, 24, 43, 46, 54, 62, 63, 64, 66, 66, 66, 66, 66, 66, 66, 66, 66, 66, 13, 21, 32, 34, 36, 43, 45, 51, 54, 55, 56, 62, 64, 66, 66, 66, 66, 66, 11, 13, 21, 22, 23, 24, 25, 26, 32, 33, 34, 35, 41, 43, 46, 53, 61, 66} ;
			// ... and test the result on the Divine table
			if (upperResult << divineTableSet[difficulty] [testLevel])
			{
				result = 7 + lowerResult ;	// success
			}
			else
			{
				result = 7 - lowerResult ;	// fail
			}

		}

	return result ;
    }
